import axios from "axios";
import { getToken, removeToken } from "./auth";

const instance = axios.create({
  // timeout: 10000,
  baseURL: import.meta.env.VITE_APP_URL,
});

// 请求拦截
instance.interceptors.request.use(
  function (config) {
    config.headers.authorization = "Bearer " + getToken();
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    return Promise.reject(error.response?.data);
  }
);

/**
 * 获取数据 get请求
 * @param {*} url
 * @param {*} config
 */
export const get = (url, config) => instance.get(url, config);

/**
 * post请求
 * @param {*} url
 * @param {*} data
 * @param {*} config
 */
export const post = (url, data) => instance.post(url, data);
/**
 * put
 * @param {*} url
 * @param {*} data
 * @param {*} config
 */
export const put = (url, data, config) => instance.put(url, data, config);

/**
 * delete
 * @param {*} url
 * @param {*} config
 */
export const remove = (url, config) => instance.delete(url, config);
